package com.handong.handserialprogram.module.setting;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.handong.handserialprogram.BuildConfig;
import com.handong.handserialprogram.R;
import com.handong.handserialprogram.module.base.BackStackFragment;
import com.handong.handserialprogram.utils.AppUtil;
import com.handong.handserialprogram.utils.SPUtil;
import com.handong.handserialprogram.utils.SystemUtil;

/**
 * AppFragment: 查看系统版本。
 */
public class AppFragment extends BackStackFragment {

    public static AppFragment newInstance() {
        AppFragment fragment = new AppFragment();
        return fragment;
    }

    @Override
    protected int getContentViewID() {
        return R.layout.fragment_app;
    }

    @Override
    protected void initViewByID(View view) {
        initHeaderLayout(R.string.nav_device_info);
        TextView pName = (TextView) view.findViewById(R.id.devicePhoneName);
        TextView pSerialNumber = (TextView) view.findViewById(R.id.devicePhoneID);
        TextView simNumber = (TextView) view.findViewById(R.id.deviceSimNumber);
        TextView simSerialNumber = (TextView) view.findViewById(R.id.deviceSimSerialNumber);
        TextView appVersion = (TextView) view.findViewById(R.id.appSystemVersion);
        TextView appUpdateTime = (TextView) view.findViewById(R.id.appUpdateTime);

        String phoneName = SPUtil.getStringValue(SPUtil.PHONE_NAME);
        pName.setText(TextUtils.isEmpty(phoneName) ? getResources().getText(R.string.nav_user_name) : phoneName);
        pSerialNumber.setText(SystemUtil.getPhoneIMEI(getContext()));

        String simSerialNumberString = SystemUtil.getSimSerialNumber(getContext());
        if (TextUtils.isEmpty(simSerialNumberString)) {
            String noSimTip = getResources().getString(R.string.device_info_no_sim_card);
            simNumber.setText(noSimTip);
            simSerialNumber.setText(noSimTip);
        } else {
            simSerialNumber.setText(simSerialNumberString);
            String simPhoneNumber = SystemUtil.getPhoneNumber(getContext());
            simNumber.setText(TextUtils.isEmpty(simPhoneNumber)
                    ? getResources().getString(R.string.device_info_get_sim_fail) : simPhoneNumber);
        }
        appVersion.setText("v" + AppUtil.getVersion(getContext())+ BuildConfig.VERSIONDATA);
        appUpdateTime.setText(SPUtil.getStringValue(SPUtil.LoginOutTime));

    }
}
